require 'date'

class CalendarController < ApplicationController

  def index
    
  end
  
  def init
    textRender = '';
    File.open("db/dayoff-2008.csv") do |file|
      while line = file.gets
        data = line.split(";")
        Calendar.new(:calendartype => data[0], :begindate => data[1].split("/")[2]+"/"+data[1].split("/")[1]+"/"+data[1].split("/")[0], :finaldate => data[2].split("/")[2]+"/"+data[2].split("/")[1]+"/"+data[2].split("/")[0], :description =>data[3]).save
      end
    end
    render :text => textRender
  end
  
  def dayoffmonth
    dayoffsJason = []
    dayoffs = Calendar.find(:all, :conditions => [ "begindate >= ? and finaldate <= ?", params[:begindate], params[:finaldate]])
    dayoffs.each { |day| dayoffsJason.push([day[:begindate].mday,'']) }
    render :text => dayoffsJason.to_json
  end
  
  def form
    if params[:act] = 'verify' and params[:date]
      @calendar = Calendar.find(:first, :conditions => [ "begindate >= ? and finaldate <= ?", params[:date], params[:date]])
      if @calendar == nil
        @calendar = Calendar.new
        params[:act] = 'create'
      else
        params[:act] = 'alter'
      end
    else
      @calendar = Calendar.new
    end
    render :update do |page|
      page.replace_html "calendar_form" , :partial => 'form'
      page.show "calendar_form"
    end      
  end
  
end
